package se.gala.core;

import java.util.Set;

import se.gala.core.abstracts.AbstractGame;
import se.gala.rng.RNG;
import se.gala.utils.GGObject;
import se.gala.utils.LongHashMap;
import se.gala.utils.UtilMap;

public class GameSessionId extends GGObject implements UtilMap {
	private final long id;
	private final AbstractGame game;
	private final static int INIT_MAP_SIZE = 1000;
	private boolean isEnded = false;
	
	private static LongHashMap map;

	
	public GameSessionId(RNG rng, AbstractGame game) {
		this.id = rng.getLongUnsigned();
		log("AbstractGame: new gameSessionId: " + this.id);
		this.game = game;
		registerSelf();
	}
	
	public long getId() {
		return this.id;
	}
	
	public final static GameSessionId get(String id) {
		return (GameSessionId) map.get(id);
	}

	public final AbstractGame getGame() {
		return game;
	}
	
	public final boolean isEnded() {
		return isEnded;
	}
	
	public void setEndGameSession() {
		// XXX mark the end of the game in the database
		isEnded = true;
		map.remove(this.getId());
	}
	
	@Override
	public final void registerSelf() {
		if (map == null) {
			map = new LongHashMap(INIT_MAP_SIZE);
		}
		map.put(this.getId(), this);
	}
	
	@Override
	public String toString() {
		return "" + this.id;
	}

	@Override
	public final Object getKey() {
		return getId();
	}

	@Override
	public Set<Long> getNamesSet() {
		return map.keySet();
	}

	@Override
	public UtilMap getMapEntry(Object key) {
		return map.get((Long) key);
	}

}
